--[[
    聚焦区域组件。在点击时后进入聚焦状态。
    ]]
FocusArea = {
    name = "FocusArea",
    dependencies = {
        SwitchButton,
    }
}

local function onReleased(self, x, y)
    if not self:hitTest(x, y) then
        if Global.focused == self then
            Global.focused = nil
        end
        self:unfocus()
    end
end
local function onSwitchOn(self)
    Global.focused = self
    EventDispatcher.global:once(NodeEvent.Released, onReleased, self)
end
function FocusArea:ctor()
    self:onSwitchOn(onSwitchOn, self)
end

function FocusArea:focus()
    Global.focused = self
    self:switch(true)
end

function FocusArea:onFocused(fn, target)
    self:on(StyleEvent.FocusChanged, fn, target)
end

function FocusArea:onFocusIn(fn, target)
    self:on(NodeEvent.FocusIn, fn, target)
end

function FocusArea:onFocusOut(fn, target)
    self:on(NodeEvent.FocusOut, fn, target)
end

function FocusArea:unfocus()
    self:switch(false)
end